package org.xiangxji.snippets.data;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.xiangxji.snippets.data.trade.TradeDownloadResult;
import org.xiangxji.snippets.data.trade.TradeSnapshot;
import org.xiangxji.snippets.data.trade.TradeSnapshotDao;

public class AnalysisTask extends TimerTask {

	private ApplicationContext context;
	private TradeSnapshotDao tradeSnapshotDao;
	@Override
	public void run() {
		try {
			init();
			
			dispacher();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public void init() {
		context = new ClassPathXmlApplicationContext(new String[]{"/spring-bean.xml"});
		tradeSnapshotDao = (TradeSnapshotDao)context.getBean("tradeSnapshotDao");
	}
	/*
	 * select seller_nick,seller_type,count(id) as amount,sum(total_fee) as fee_all from trade_download_report_1
where created='2010-12-21' and seller_nick in ('tearing_angel', '粉红大布娃娃', '七格格吉祥', 'vicky0vicky', 'kamozi') and total_fee<10000000
	 */
	public void dispacher() throws IOException {
		writeLog("卖家昵称,卖家类型,GMV笔数,GMV总额");
		try {
			Map<Long,TradeSnapshot> all = new HashMap<Long,TradeSnapshot>();
			for(int i = 0;i<9;i++) {
				String sqls = "SELECT SELLER_NICK,SELLER_TYPE,TID,TOTAL_FEE FROM TRADE_DOWNLOAD_REPORT_" + i + " WHERE CREATED='2010-12-21' AND SELLER_NICK IN ('tearing_angel', '粉红大布娃娃', '七格格吉祥', 'vicky0vicky', 'kamozi') AND TOTAL_FEE<10000000";
				List<TradeSnapshot> result = tradeSnapshotDao.getData(sqls);
				for(TradeSnapshot t : result) {
					if(!all.containsKey(t.getTid())) {
						all.put(t.getTid(), t);
					}
				}
				System.out.println(result.size());
			}

			Map<String,TradeDownloadResult> analysis = new HashMap<String,TradeDownloadResult>();

			Iterator<Long> i = all.keySet().iterator();
			while(i.hasNext()) {
				Long key = i.next();
				TradeSnapshot t = all.get(key);
				if(analysis.containsKey(t.getSeller_nick())) {
					TradeDownloadResult r = analysis.get(t.getSeller_nick());
					r.setAmount(r.getAmount()+1);
					r.setFee_all(r.getFee_all()+t.getTotal_fee());
					analysis.put(t.getSeller_nick(), r);
				} else {
					TradeDownloadResult r = new TradeDownloadResult();
					r.setAmount(1);
					r.setFee_all(t.getTotal_fee());
					r.setSellerType(t.getSeller_type());
					analysis.put(t.getSeller_nick(), r);
				}
			}
			Iterator<String> j = analysis.keySet().iterator();
			while(j.hasNext()) {
				String key = j.next();
				TradeDownloadResult t = analysis.get(key);
				writeLog(key+","+t.getSellerType()+","+t.getAmount()+","+t.getFee_all());
				System.out.println(key+","+t.getSellerType()+","+t.getAmount()+","+t.getFee_all());
			}
		}catch(SQLException e) {
			e.printStackTrace();
		}

	}
	public void writeLog(String s) {
		String filename = "D:\\\\backup\\\\result.csv";
		File file = new File(filename);
		if(!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		RandomAccessFile rf;
		try {
			rf = new RandomAccessFile(filename,"rw");//定义一个类RandomAccessFile的对象，并实例化
			rf.seek(rf.length());//将指针移动到文件末尾
			rf.writeBytes(s+"\n");
			rf.close();//关闭文件流
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
//	@Test
//	public void testWriterFile() {
//		writeLog("hello world");
//		writeLog("hello,world");
//	}
//	@Test
//	public void testD() throws IOException { 
//		init();
//		dispacher();
//	}
}
